package com.excel.validators.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.excel.validators.core.decorative.AbstractSheet;
import com.excel.validators.core.decorative.CompositeValue;
import com.excel.validators.core.decorative.Validator;
import com.excel.validators.core.decorative.inner.ImportEntry;
import com.excel.validators.core.extension.ModelAnalysis;

/**
 * @author wangxiaoliang10
 * 
 * 读取excel数据转换实体模型
 */
public abstract class ExcelReadHandler extends AbstractSheet implements ImportEntry {
	
	/**
	 * 从第几行开始处理
	 */
	private int skipRows = 1;
	
	/*
	 * 模板工作表
	 */
	private XSSFSheet xssfSheet;

	public ExcelReadHandler() {
			super();
	}
	
	/**
	 * 构造器
	 * @param uploadExcel
	 */
	public ExcelReadHandler(File excelFile, int worksheet) {
			nullCheck(excelFile, worksheet); // check null argument
	}

	/**
	 * 构造器
	 * @param uploadExcel
	 */
	public ExcelReadHandler(InputStream inputStream) {
			loadExcel(inputStream, 0);
	}
	
	/**
	 * 获取excel工作簿的促销表
	 * @return
	 */
	protected XSSFSheet getSheet() {
			return xssfSheet;
	}
	
	public void setSheet(XSSFSheet xssfSheet) {
			this.xssfSheet = xssfSheet;
	}
	
	/**
	 * 对excel进行数据进行实例化
	 * @param excelFile
	 * @throws IOException 
	 */
	private void nullCheck(File excelFile, int worksheet) {
			InputStream inputStream;
			try {
					// null argument check
					if (excelFile == null || 
							(inputStream = new FileInputStream(excelFile)) == null) {
							throw new IllegalStateException("Could not load excel file :" + excelFile);
					}
					loadExcel(inputStream, worksheet);
			} catch (Exception e) {
					throw new IllegalArgumentException(
							"excel inputstream process error : " + e.getMessage());
			}
	}

	/**
	 * load excel file 
	 * @throws IOException 
	 */
	protected void loadExcel(InputStream inputStream, int worksheet) {
			try {
					XSSFWorkbook hssfWorkbook = new XSSFWorkbook(inputStream);
					this.xssfSheet = hssfWorkbook.getSheetAt(worksheet);
			} catch (IOException e) {
					logger.error("create XSSFWorkbook object is error. ", e);
					throw new IllegalArgumentException(
							"Could not load inputStream : " + e.getMessage());
			}
	}

	@Override
	protected List<CompositeValue> createCompositeValues() {
			// TODO 从excel中解析数据并生成检查器
			XSSFSheet sheet = getSheet();
			// 计算excle中行数
			int maxSize = sheet.getLastRowNum();
			// 初始化List
			List<CompositeValue> compositeValues = new ArrayList<CompositeValue>(maxSize);
			// 循环行Row,排除第一行title
			for (int rowNum = skipRows; rowNum <= maxSize; rowNum++) {
					XSSFRow hssfRow = sheet.getRow(rowNum);
					if (hssfRow == null) {
							continue;
					}
					CompositeValue composite = buildComposite(hssfRow);
					if (logger.isTraceEnabled()) {
							logger.info(composite.toString());
					}
					compositeValues.add(composite);
			}
			return compositeValues;
	}
	
	/**
	 * excel行处理,生成受检对象
	 * @param hssfRow
	 * @return
	 */
	protected abstract CompositeValue buildComposite(XSSFRow xssrow);
	
	/**
	 * 设置从第几行开始处理，默认是从第0行
	 */
	public int getSkipHeader() {
			return skipRows;
	}
	
	@Override
	public void skipHeader(int row) {
			this.skipRows = row;
	}

	@Override
	public void setValidator(Validator validator) {
			ModelAnalysis analysis = get();
			if (analysis != null) {
					analysis.setValidator(validator);
			}
	}
}